package com.lninl.controller;

import com.lninl.model.User;
import com.lninl.repository.UserRepository;
import com.lninl.service.UserService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {

        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doLogin() {
        System.out.println(userRepository.findByUsername("qlp"));
        return "login";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public
    @ResponseBody
    String logout() {
        return "logout ok";
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register() {
        return "register";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public User doRegister(@Valid User user) {
        //user.setId(1L);
        user.setStatus(User.NORMAL);
        user.setCreated(new DateTime());
        return userRepository.save(user);
        //return "register ok1";
    }

    @RequestMapping(value = "/forget", method = RequestMethod.GET)
    public String forget() {
        return "forget";
    }

    @RequestMapping(value = "/forget", method = RequestMethod.POST)
    @ResponseBody
    public String doForget() {
        return "forget ok";
    }

    @RequestMapping(value = {"/", "/home"}, method = RequestMethod.GET)
    public String home(Model model) {
        return "home";
    }

}
